//
// $Id: AXWindowHandler.m 6 2010-08-05 19:43:27Z principiadriver $
//
// Written by
//  Michael Klein <michael.klein@puffin.lb.shuttle.de>
//
// This file is part of Alternate Moves.
//
// This  is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#import <ApplicationServices/ApplicationServices.h>
#import "WindowHandler.h"


static BOOL axElementIsWindowRole(AXUIElementRef element)
{
    CFTypeRef role;
    BOOL ret;
    if (AXUIElementCopyAttributeValue(element, kAXRoleAttribute, &role) == kAXErrorSuccess) {
        ret = (CFStringCompare(role, kAXWindowRole, 0) == 0);
        CFRelease(role);
    }
    return ret;
}


@interface AXWindowHandler : NSObject<WindowHandler>
{
    AXUIElementRef axwindow;
}

@end

@implementation AXWindowHandler

- (id)initWithAXWindow:(AXUIElementRef)window
{
    if ((self = [super init]) != nil) {
        axwindow = window;
    }

    return self;
}


- (void)dealloc
{
    CFRelease(axwindow);
    [super dealloc];
}


+ (id)newWindowHandlerForWindowAtPoint:(CGPoint)loc
{
    AXUIElementRef axref = AXUIElementCreateSystemWide();
    if (axref != NULL) {
        AXUIElementRef axelement;
        AXError err = AXUIElementCopyElementAtPosition(axref, loc.x, loc.y, &axelement);
        CFRelease(axref);
        if (err == kAXErrorSuccess) {
            while(axelement) {
                if (axElementIsWindowRole(axelement)) {
                    return [[AXWindowHandler alloc] initWithAXWindow:axelement];
                } else {
                    AXUIElementRef topLeveUIElement;
                    if (AXUIElementCopyAttributeValue(axelement, kAXTopLevelUIElementAttribute,
                                                      (CFTypeRef*)&topLeveUIElement) == kAXErrorSuccess) {
                        CFRelease(axelement);
                        if (axElementIsWindowRole(topLeveUIElement)) {
                            return [[AXWindowHandler alloc] initWithAXWindow:topLeveUIElement];
                        } else {
                            CFRelease(topLeveUIElement);
                            return nil;
                        }
                    }
                    else
                    {
                        AXUIElementRef parent;
                        if (AXUIElementCopyAttributeValue(axelement, kAXParentAttribute,
                                                          (CFTypeRef*)&parent) == kAXErrorSuccess) {
                            CFRelease(axelement);
                            if (axElementIsWindowRole(parent)) {
                                return [[AXWindowHandler alloc] initWithAXWindow:parent];
                            } else {
                                axelement = parent;
                            }
                        } else {
                            return nil;
                        }
                    }
                }
            }
        }
    } else {
        NSLog(@"AXUIElementCreateSystemWide() failed");
    }
    return nil;
}

- (CGRect)setWindowRect:(CGRect)newRect
{
    CFTypeRef value;

    value = AXValueCreate(kAXValueCGSizeType, &newRect.size);
    if (value) {
        AXUIElementSetAttributeValue(axwindow, kAXSizeAttribute, value);
        CFRelease(value);
    }

    value = AXValueCreate(kAXValueCGPointType, &newRect.origin);
    if (value) {
        AXUIElementSetAttributeValue(axwindow, kAXPositionAttribute, value);
        CFRelease(value);
    }

    return [self windowRect];
}


- (CGRect)windowRect
{
    AXError err;
    CFTypeRef value;
    CGRect rect = CGRectZero;

    err = AXUIElementCopyAttributeValue(axwindow, kAXSizeAttribute, &value);
    if (err == kAXErrorSuccess) {
        AXValueGetValue(value, kAXValueCGSizeType, &rect.size);
        CFRelease(value);
    }

    err = AXUIElementCopyAttributeValue(axwindow, kAXPositionAttribute, &value);
    if (err == kAXErrorSuccess) {
        AXValueGetValue(value, kAXValueCGPointType, &rect.origin);
        CFRelease(value);
    }

    return rect;
}


- (NSString*)windowTitle
{
    AXError err;
    CFTypeRef value;

    err = AXUIElementCopyAttributeValue(axwindow, kAXTitleAttribute, &value);
    if (err == kAXErrorSuccess) {
        NSString * ret = [NSString stringWithString:(NSString*)value];
        CFRelease(value);
        return ret;
    }
    return @"";
}


@end
